<template>
  <div class="header">
    <div class="h-top">
      <div class="w">
        <div class="logo" @click="to('/home')"></div>
        <ul>
          <li :class="topNavIdx==1?'active':''" @click="to('/home')">发现音乐<i v-show="topNavIdx==1"></i></li>
          <li :class="topNavIdx==2?'active':''" @click="to('/mymusic')">我的音乐<i v-show="topNavIdx==2"></i></li>
          <li :class="topNavIdx==3?'active':''">关注<i v-show="topNavIdx==3"></i></li>
          <li :class="topNavIdx==4?'active':''">商城<i v-show="topNavIdx==4"></i></li>
          <li :class="topNavIdx==5?'active':''">音乐人<i v-show="topNavIdx==5"></i></li>
          <li :class="topNavIdx==6?'active':''">下载客户端<i v-show="topNavIdx==6"></i><em></em></li>
        </ul>
        <div class="login-a">
          <router-link v-if="!token" to="/login">登录</router-link>
          <el-dropdown v-else :hide-on-click="false">
            <span class="el-dropdown-link">
              <img :src="userInfo.avatar">
            </span>
            <el-dropdown-menu slot="dropdown" style="background-color: rgb(36,36,36); border-color: #353535; width: 138px; text-align: center;">
              <el-dropdown-item class="login-menu-item">
                <img class="icon1" src="./images/主页.png">
                <router-link to="/user/homepage" class="login-menu-a">我的主页</router-link>
              </el-dropdown-item>
              <el-dropdown-item class="login-menu-item">
                <img src="./images/消息中心.png" alt="#" class="icon2">
                <router-link to="/mymessages" class="login-menu-a">我的消息</router-link>
              </el-dropdown-item>
              <el-dropdown-item class="login-menu-item">
                <img src="./images/设置.png" alt="#" class="icon3">
                <router-link to="/setting" class="login-menu-a">个人设置</router-link>
              </el-dropdown-item>
              <el-dropdown-item class="login-menu-item">
                <img src="./images/退出.png" alt="#" class="icon4">
                <span @click="logout" style="color: #dedede;" class="login-menu-a">退出</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="creator-center">
          <router-link to="">创作者中心</router-link>
        </div>
        <div class="search-input">
          <div class="si-bg">
            <input type="text" placeholder="音乐/视频/电台/用户" v-model="keyword" @keyup.enter="toSearch">
          </div>
        </div>
      </div>
    </div>
    <div class="h-bottom">
      <div class="w">
        <ul v-show="bottomNavIdx !== ''">
          <li :class="bottomNavIdx==1?'active':''" @click="to('/home')">推荐</li>
          <li :class="bottomNavIdx==2?'active':''" @click="to('/ranking')">推荐榜</li>
          <li :class="bottomNavIdx==3?'active':''" @click="to('/playlist')">歌单<i></i></li>
          <li :class="bottomNavIdx==4?'active':''">主播电台</li>
          <li :class="bottomNavIdx==5?'active':''" @click="to('/singer')">歌手</li>
          <li :class="bottomNavIdx==6?'active':''">新碟上架</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Header',
  data () {
    return {
      topNavIdx: 1,
      bottomNavIdx: 1,
      keyword: ''
    }
  },
  created() {
    if(sessionStorage.getItem('currentNavIdx')) {
      const currentNavIdx = JSON.parse(sessionStorage.getItem('currentNavIdx'))
      this.topNavIdx = currentNavIdx.top
      this.bottomNavIdx = currentNavIdx.bottom
    } else {
      sessionStorage.setItem('currentNavIdx', JSON.stringify({top: 1, bottom: 1}))
    }
  },
  watch: {
    $route(to, from) {
      if(to.path === '/home') {
        this.updateIdx(1, 1)
      } else if(to.path === '/ranking') {
        this.updateIdx(1, 2)
      } else if(to.path === '/playlist') {
        this.topNavIdx = 1
        this.bottomNavIdx = 3
        sessionStorage.setItem('currentNavIdx', JSON.stringify({top: 1, bottom: 3}))
      } else if(to.path === '/singer') {
        this.topNavIdx = 1
        this.bottomNavIdx = 5
        sessionStorage.setItem('currentNavIdx', JSON.stringify({top: 1, bottom: 5}))
      } else if(to.path === '/mymusic') {
        this.updateIdx(2, '')
      } else {
        this.updateIdx('', '')
      }
    }
  },
  methods: {
    toSearch() {
      if(this.keyword !== '') {
        this.$router.push({
          path: `/searchPage/${this.keyword}`
        })
        this.keyword = ''
      }
    },
    updateIdx(top, bottom) {
      this.topNavIdx = top
      this.bottomNavIdx = bottom
      sessionStorage.setItem('currentNavIdx', JSON.stringify({top, bottom}))
    },
    to(path) {
      this.$router.push({ path })
    },
    findMusic(){
      this.topNavIdx = 1;
      this.$router.push({path:'/home'})
    },
    myMusic(){
      this.topNavIdx = 2;
      this.$router.push({path:'/mymusic'})
    },
    logout() {
      this.$store.commit('USERLOGIN', null)
      this.$store.commit('SET_USERINFO', { name: '未登录' })
      localStorage.removeItem('TOKEN')
      localStorage.removeItem('music-userInfo')
      this.$router.push({
        path: '/home'
      })
      this.$message.success('退出成功')
    }
  },
  computed: {
    token() {
      return this.$store.state.user.token
    },
    userInfo() {
      return this.$store.state.user.userInfo
    }
  }
}
</script>

<style lang="less" scoped>
.w {
  height: 100%;
}
.header {
  width: 100%;
  .h-top {
    width: 100%;
    height: 70px;
    background-color: #242424;
    box-sizing: border-box;
    .logo {
      float: left;
      padding-right: 20px;
      width: 157px;
      height: 100%;
      background-position: 0 0;
      background: url(./images/topbar.png) no-repeat 0 0;
      cursor: pointer;
    }
    ul {
      float: left;
      display: flex;
      li {
        position: relative;
        padding: 0 19px;
        line-height: 70px;
        color: #ccc;
        font-size: 14px;
        cursor: pointer;
        i {
          position: absolute;
          left: 50%;
          margin-left: -6px;
          margin-top: -6px;
          display: block;
          width: 12px;
          height: 7px;
          background: url(./images/topbar.png) no-repeat -226px 0;
        }
        em {
          display: block;
          position: absolute;
          top: 21px;
          left: 100px;
          width: 28px;
          height: 19px;
          background: url(./images/topbar.png) no-repeat -190px 0;
        }
        &:hover {
          color: #fff;
        }
      }
      .active {
        color: #fff;
        background-color: #000;
      }
    }
    .login-a {
      float: right;
      margin-left: 20px;
      z-index: 1;
      a {
        line-height: 70px;
        color: #787878;
        text-decoration: none;
        &:hover {
          color: #666;
        }
      }
      .el-dropdown-link {
        img {
          margin-top: 20px;
          width: 30px;
          border-radius: 50%;
          cursor: pointer;
        }
      }
    }
    .creator-center {
      float: right;
      a {
        display: inline-block;
        width: 90px;
        height: 32px;
        margin: 19px 0 0 12px;
        box-sizing: border-box;
        text-align: center;
        border: 1px solid #4f4f4f;
        line-height: 32px;
        color: #ccc;
        border-radius: 20px;
      }
    }
    .search-input {
      float: right;
      margin-top: 19px;
      width: 158px;
      height: 32px;
      border-radius: 32px;
      background-color: #fff;
      .si-bg {
        height: 100%;
        background: url(./images/topbar.png) no-repeat 0 -99px;
        input {
          float: right;
          margin-top: 8px;
          padding-left: 20px;
          width: 95%;
          line-height: 16px;
          border: none;
          color: #333;
          opacity: 1;
          background-color: transparent;
          box-sizing: border-box;
          outline: none;
        }
      }
    }
  }
  .h-bottom {
    min-height: 5px;
    background-color: #c20c0c;
    ul {
      display: flex;
      margin-left: 180px;
      height: 35px;
      li {
        position: relative;
        margin: 7px 17px 0;
        padding: 0 13px;
        height: 20px;
        border-radius: 20px;
        line-height: 21px;
        color: #fff;
        cursor: pointer;
        i {
          position: absolute;
          display: block;
          width: 8px;
          height: 8px;
          top: 2px;
          right: 6px;
          background: url(./images/white-r-icon@3x.png) no-repeat 0% 0%;
          background-size: cover;
        }
        &:hover {
          background-color: #9b0909;
        }
      }
      .active {
        background-color: #9b0909;
      }
    }
  }
}
/deep/ .popper__arrow::after {
  border-bottom-color: #353535 !important;
}
.login-menu-item {
  position: relative;
  a {
    font-size: 12px;
    color: rgb(222, 222, 222) !important;
  }
  .icon1{
    width: 18px;
    position: absolute;
    left: 20px;
    top: 8px;
  }
  .icon2{
    width: 18px;
    position: absolute;
    left: 20px;
    top: 8px;
  }
  .icon3{
    width: 18px;
    position: absolute;
    left: 20px;
    top: 8px;
  }
  .icon4{
    width: 18px;
    position: absolute;
    left: 20px;
    top: 8px;
  }
  &:hover {
    background-color: #353535 !important;
    /deep/ a {
      color: #fff !important;
    }
  }
}
</style>